﻿using DrawTools.Tools;
using LS.Screening.UI.Common;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Media;

namespace DrawTools
{
    /// <summary>
    /// 浏览工具配置
    /// </summary>
    [ConfigFileName("Config_LS.DicomViewer.DrawerConfig.json")]
    public class DrawerConfig : FileConfig<DrawerConfig>
    {
        /// <summary>
        /// 默认字号
        /// </summary>
        [Reactive]
        public double FontSize { get; set; } = 12;

        /// <summary>
        /// 默认颜色
        /// </summary>
        [Reactive]
        public SolidColorBrush DefaultBrush { get; set; } = new SolidColorBrush(Colors.Red);

        /// <summary>
        /// 默认线宽
        /// </summary>
        [Reactive]
        public UInt32 StrokeThickness { get; set; } = 1;

        /// <summary>
        /// 是：点线，否：直线
        /// </summary>
        [Reactive]
        public bool IsDashStyle { get; set; } = true;

        /// <summary>
        /// 是否标记序号
        /// </summary>
        [Reactive]
        public bool ShowDrawIndex { get; set; } = true;

        /// <summary>
        /// 文本是否一起显示
        /// </summary>
        [Reactive]
        public bool TextShowTogether { get; set; } = true;

        public List<DrawTypeItem> DrawTypeItems { get; set; }

        public DrawerConfig()
        {
            DrawTypeItems = new List<DrawTypeItem>();
        }

        protected override void OnCreated()
        {
            base.OnCreated();
            if (DrawTypeItems == null)
            {
                DrawTypeItems = new List<DrawTypeItem>();
            }
            var type = typeof(DrawToolType);
            try
            {
                if (DrawTypeItems.Count > 0)
                {
                    for (int i = 0; i < DrawTypeItems.Count; i++)
                    {
                        var item = DrawTypeItems[i];
                        var att = type.GetField(item.Type.ToString()).GetCustomAttribute<ItemVisable>();
                        if (att == null || att.Visable == false)
                        {
                            DrawTypeItems.RemoveAt(i);
                            i--;
                        }
                    }
                }
                var items = EnumHelper.GetItemDescriptions<DrawToolType>();
                foreach (var item in items)
                {
                    var att = type.GetField(item.Value.ToString()).GetCustomAttribute<ItemVisable>();
                    if (att == null || att.Visable == false) continue;
                    if (!DrawTypeItems.Any(p => p.Type.Equals(item.Value)))
                    {
                        DrawTypeItems.Add(new DrawTypeItem { Type = (DrawToolType)item.Value, IsUsed = true, Description = item.Description });
                    }
                }
                DrawTypeItems = DrawTypeItems.OrderBy(p => (int)p.Type).ToList();
                Save();
            }
            catch { }
        }

        public static IEnumerable<object> TrueTypes
        {
            get
            {
                if (Instance != null)
                {
                    var ret = Instance.DrawTypeItems.Where(p => p.IsUsed == true).Select(p => (object)p.Type);
                    if (ret.Count() == 0) return Instance.DrawTypeItems.Select(p => (object)p.Type);
                    return ret;
                }
                return Enumerable.Empty<object>();
            }
        }
    }
}